package co.yixiang.modules.system.service.impl;

import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.modules.system.domain.PartnerImage;
import co.yixiang.modules.system.dto.PartnerImageDto;
import co.yixiang.modules.system.dto.queryCriteria.PartnerImageQueryCriteria;
import co.yixiang.modules.system.mapper.PartnerImageMapper;
import co.yixiang.modules.system.service.PartnerImageService;
import co.yixiang.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @author zf
* @date 2021-10-11
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "partnerImage")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class PartnerImageServiceImpl extends BaseServiceImpl<PartnerImageMapper, PartnerImage> implements PartnerImageService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(PartnerImageQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<PartnerImage> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(PartnerImageDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<PartnerImage> queryAll(PartnerImageQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(PartnerImage.class, criteria));
    }


    @Override
    public void download(List<PartnerImageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PartnerImageDto partnerImage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("模版名称", partnerImage.getName());
            map.put("图片地址", partnerImage.getImgUrl());
            map.put("文案一", partnerImage.getClerkOne());
            map.put("文案二", partnerImage.getClerkTwo());
            map.put("创建时间", partnerImage.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
